package com.smartfactory.sail.controller;


import com.ada.base.common.ApiResponse;
import com.ada.base.common.ControllerResponse;
import com.ada.base.common.ResultData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smartfactory.sail.config.OperRecord;
import com.smartfactory.sail.entity.Dept;
import com.smartfactory.sail.entity.DeptPost;
import com.smartfactory.sail.service.IDeptPostService;
import com.smartfactory.sail.service.IDeptService;
import com.smartfactory.sail.vo.dept.ListAllPostResp;
import com.smartfactory.sail.vo.dept.ListDeptResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author 王巨壁
 * @since 2019-07-24
 */
@Api(tags ="部门相关")
@RestController
@RequestMapping("/deptAndPost")
public class DeptController extends ControllerResponse {

    private IDeptService iDeptService;
    private IDeptPostService iDeptPostService;

    public DeptController(IDeptService iDeptService, IDeptPostService iDeptPostService) {
        this.iDeptService = iDeptService;
        this.iDeptPostService = iDeptPostService;
    }

    @OperRecord(content = "添加新部门",level = 1)
    @PostMapping("addDept")
    @ApiOperation(value = "部门新增",notes = "部门新增")
    public ApiResponse<Long> add(@RequestBody Dept dept) {
        QueryWrapper<Dept> query = new QueryWrapper<>();
        query.eq("deptName", dept.getDeptName());
        if (iDeptService.count(query) > 0) {
            return fail("部门已经存在,,请重新创建!");
        }
        if (iDeptService.add(dept) != null) {
            return ok(dept.getId());
        }
        return fail(ResultData.FAIL_CODE_MSG);
    }

    @OperRecord(content = "删除部门",level = 1)
    @PostMapping("delDept/{id}")
    @ApiOperation(value = "部门删除 岗位一起删除",notes = "部门删除 岗位一起删除")
    public ApiResponse<Boolean> del(@PathVariable("id")Long id) {
        if (iDeptService.del(id)) {
            return ok(true);
        }
        return fail(ResultData.FAIL_CODE_MSG);
    }

    @OperRecord(content = "修改部门",level = 1)
    @PostMapping("updateDept")
    @ApiOperation(value = "部门修改",notes = "部门修改")
    public ApiResponse<Boolean> update(@RequestBody Dept dept) {
        if (iDeptService.updateById(dept)) {
            return ok(true);
        }
        return fail(ResultData.FAIL_CODE_MSG);
    }



    @PostMapping("listDept")
    @ApiOperation(value = "部门列表  无分页", notes = "部门列表 无分页")
    public ApiResponse<List<ListDeptResp>> list() {
        List<Dept> depts = iDeptService.list();
        List<DeptPost> posts = iDeptPostService.list();
        List<ListDeptResp> listDeptResps = new ArrayList<>();
        for (Dept dept : depts) {
            ListDeptResp listDeptResp = new ListDeptResp();
            listDeptResp.setDeptName(dept.getDeptName());
            listDeptResp.setId(dept.getId());
            listDeptResp.setSort(dept.getSort());
            ArrayList<DeptPost> deptPosts = new ArrayList<>();
            for (DeptPost post : posts) {
                if (dept.getId() == post.getDeptId()) {
                    deptPosts.add(post);
                }
            }
            listDeptResp.setPosts(deptPosts);
            listDeptResps.add(listDeptResp);
        }
        return ok(listDeptResps);
    }

    @ApiOperation(value = "根据部门id获取部门岗位详情", notes = "根据部门id获取部门岗位详情")
    @GetMapping("infoDept/{id}")
    public ApiResponse<Dept> getById(@PathVariable Long id) {
        return ok(iDeptService.getById(id));
    }

    //-----------------------------------------------------------------------------------------------
    @OperRecord(content = "新增部门岗位",level = 1)
    @PostMapping("addPost")
    @ApiOperation(value = "岗位新增",notes = "岗位新增")
    public ApiResponse<Long> addPost(@RequestBody DeptPost deptPost) {

        QueryWrapper<DeptPost> query = new QueryWrapper<>();
        query.eq("deptId",deptPost.getDeptId());
        query.eq("postName",deptPost.getPostName());
        if (iDeptPostService.count(query) > 0) {
            return fail("岗位已经存在,请重新创建!");
        }

        if (iDeptPostService.save(deptPost)) {
            return ok(deptPost.getId());
        }
        return fail(ResultData.FAIL_CODE_MSG);
    }

    @OperRecord(content = "删除岗位",level = 1)
    @PostMapping("delPost/{id}")
    @ApiOperation(value = "岗位删除",notes = "岗位删除")
    public ApiResponse<Boolean> delPost(@PathVariable("id")Long id) {
        if (iDeptPostService.removeById(id)) {
            return ok(true);
        }
        return fail(ResultData.FAIL_CODE_MSG);
    }

    @OperRecord(content = "修改岗位",level = 1)
    @PostMapping("updatePost")
    @ApiOperation(value = "修改岗位名称或者排序 deptId,id,name,sort都必传",notes = "修改岗位名称或者排序 deptId,id,name,sort都必传")
    public ApiResponse<Boolean> updatePost(@RequestBody DeptPost deptPost) {
        if (iDeptPostService.updateById(deptPost)) {
            return ok(true);
        }
        return fail(ResultData.FAIL_CODE_MSG);
    }

    @PostMapping("listPost/{deptId}")
    @ApiOperation(value = "部门下岗位列表(传部门id)  无分页", notes = "部门下岗位列表(传部门id)  无分页")
    public ApiResponse<List<DeptPost>> listPost(@PathVariable("deptId") Long deptId) {
        QueryWrapper<DeptPost> query = new QueryWrapper<>();
        query.eq("deptId",deptId).orderByAsc("sort");
        return ok(iDeptPostService.list(query));
    }

    @ApiOperation(value = "根据岗位id获取部门岗位详情", notes = "根据岗位id获取部门岗位详情")
    @GetMapping("infoPost/{id}")
    public ApiResponse<DeptPost> infoPost(@PathVariable Long id) {
        return ok(iDeptPostService.getById(id));
    }

    @ApiOperation(value = "获取所有岗位的list", notes = "获取所有岗位的list")
    @GetMapping("getAllPost")
    public ApiResponse<List<ListAllPostResp>> getAllPost() {

        return ok(iDeptPostService.getAllPost());
    }

}
